<?php

namespace addons\aqueue\library\jobs;

use think\Db;
use think\Log;
use think\queue\Job;

class ProductToShopify
{
    public function fire(Job $job, $data)
    {
        // products_id   shopify_url
        // Log::write('测试日志信息,这是警告级别，并且实时写入','notice');die;
        //循环id处理数据
        if (!is_array($data['products_id'] || empty($data['shopify_url']))) {
            $job->delete();
        }
        foreach ($data['products_id'] as $v) {
            $this->product_to_shopify($v, $data['shopify_url']);
        }
        //检查任务重试次数
        if ($job->attempts() > 3) {
            $job->delete();
        }
        $job->delete(); //任务执行成功后删除
    }

    //新增商品到店铺
    public function product_to_shopify($id, $params)
    {
        $products_shopify = Db::name('products_shopify')
            ->where('id', $id)
            ->find();
        $msgs = 3;

        if (!empty($products_shopify)) {
            // if($products_shopify['accounturl'] == $params){
            //     $msgs = 2;
            // }else{
            //先更新一遍再生成SKU
            $iscustom = Db::name('fedition_accounturl')
                ->where('id', $products_shopify['accounturl'])
                ->find();
            $url = $iscustom['shopifyname'] . "/admin/api/2022-10/products/" . $products_shopify['id'] . ".json";
            $ldata =  geturl($url, $iscustom['shopifyapikey'], $iscustom['shopifysecretapikey']);
            $larr = array();
            if ($ldata) {
                $ldata2 = json_decode($ldata, true);
                if (!empty($ldata2['product'])) {
                    $lproduct = $ldata2['product'];
                    $larr['title'] = $lproduct['title'];
                    $larr['body_html'] = $lproduct['body_html'];
                    $larr['vendor'] = $lproduct['vendor'];
                    $larr['product_type'] = $lproduct['product_type'];
                    $larr['tags'] = $lproduct['tags'];
                    $larr['status'] = $lproduct['status'];
                    $larr['variants'] = array();
                    $larr['options'] = array();
                    $larr['images'] = array();
                    if (!empty($lproduct['variants'])) {
                        foreach ($lproduct['variants'] as $kvariants => $vvariants) {
                            $avariants = array();
                            $avariants['option1'] = $vvariants['option1'];
                            $avariants['price'] = $vvariants['price'];
                            $avariants['sku'] = $vvariants['sku'];
                            $avariants['option1'] = $vvariants['option1'];
                            $avariants['option2'] = $vvariants['option2'];
                            $avariants['option3'] = $vvariants['option3'];
                            $larr['variants'][] = $avariants;
                        }
                    }
                    if (!empty($lproduct['options'])) {
                        foreach ($lproduct['options'] as $koptions => $voptions) {
                            $aoptions = array();
                            $aoptions['name'] = $voptions['name'];
                            if (!empty($voptions['values'])) {
                                $aoptions['values'] = implode(',', $voptions['values']);
                            }
                            $larr['options'][] = $aoptions;
                        }
                    }
                    if (!empty($lproduct['images'])) {
                        foreach ($lproduct['images'] as $kimages => $vimages) {
                            $aimages = array();
                            $aimages['src'] = $vimages['src'];
                            $larr['images'][] = $aimages;
                        }
                    }
                }
            }
            if (!empty($larr)) {
                $newdata['product'] = $larr;
                $iscustom2 = Db::name('fedition_accounturl')
                    ->where('id', $params)
                    ->find();
                $url2 = $iscustom2['shopifyname'] . "/admin/api/2022-10/products.json";
                $newreturn = posturl($url2, $newdata, $iscustom2);

                if (!empty($newreturn)) {
                    $datareturn = json_decode($newreturn, true);
                    if (isset($datareturn['product']['id']) && !empty($datareturn['product']['id'])) {
                        $msgs = $datareturn['product']['id'];
                    }
                }
            }
            // }
            return $msgs;
        } else {
            return $msgs;
        }
    }
}
